package com.System.service.BarImpl;

import com.System.Dao.BarDao;
import com.System.pojo.*;
import com.System.service.Bar;
import com.System.untils.SqlSessionFactoryUtils;
import com.cloudHouse.session.SqlSession;
import com.cloudHouse.session.SqlSessionFactory;

import java.util.List;

public class BarService implements Bar {
    SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();
    @Override
    public com.System.pojo.Bar selectOne(String name) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);

        com.System.pojo.Bar bar = mapper.selectOne(name);
        sqlSession.close();
        return bar;
    }

    @Override
    public List<Post> selectAllPosts(Post post) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);

        List<Post> posts = mapper.selectAllPosts(post);
        sqlSession.close();
        return posts;

    }

    @Override
    public List<Post> selectOutOfMyPosts(Post post) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);

        List<Post> posts = mapper.selectOutOfMyPosts(post);
        sqlSession.close();
        return posts;
    }

    @Override
    public void addFocus(User_Bar_Relationship userBarRelationship) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        mapper.addFocus(userBarRelationship);
        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public User_Bar_Relationship hasFocus(User_Bar_Relationship userBarRelationship) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        //sqlSession.close();
        User_Bar_Relationship userBarRelationship1 = mapper.hasFocus(userBarRelationship);
        sqlSession.close();
        return userBarRelationship1;
    }

    @Override
    public void deleteFocus(User_Bar_Relationship userBarRelationship) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        mapper.deleteFocus(userBarRelationship);
        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public void addPinPost(Post post) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        mapper.addPinPost(post);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Post selectPinPost(String name) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        Post pinPost = mapper.selectPinPost(name);
        sqlSession.close();
        return pinPost;
    }

    @Override
    public void deletePinPost(String name) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        mapper.deletePinPost(name);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Post hasPinPost(String name) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        Post post = mapper.hasPinPost(name);
        sqlSession.close();
        return post;
    }

    @Override
    public void updateBarOwner(User_Bar_Relationship userBarRelationship) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        mapper.updateBarOwner(userBarRelationship);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void addRelationShip(User_Bar_Relationship userBarRelationship) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        mapper.addRelationShip(userBarRelationship);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateBarTableOwner(User_Bar_Relationship userBarRelationship) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        mapper.updateBarTableOwner(userBarRelationship);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateNowNumber(com.System.pojo.Bar bar) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        mapper.updateNowNumber(bar);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public com.System.pojo.Bar selectNowNumber(com.System.pojo.Bar bar) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        com.System.pojo.Bar result = mapper.selectNowNumber(bar);
        sqlSession.close();
        return result;
    }

    @Override
    public Ban hasBan(Ban ban) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        Ban result = mapper.hasBan(ban);
        sqlSession.close();
        return result;
    }

    @Override
    public void deleteBan(Ban ban) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        mapper.deleteBan(ban);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void addBan(Ban ban) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        mapper.addBan(ban);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteReport(Report report) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        mapper.deleteReport(report);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Report> selectAllReport(Report report) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        List<Report> reports = mapper.selectAllReport(report);
        sqlSession.close();
        return reports;
    }

    @Override
    public void addReport(Report report) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        mapper.addReport(report);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Report hasReport(Report report) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BarDao mapper = sqlSession.getMapper(BarDao.class);
        Report result = mapper.hasReport(report);
        sqlSession.close();
        return result;
    }


}
